/* **************************************************************

   Profile.cpp:
   Access to text ini files
   
   ************************************************************** 

   NSim: simulation of a neutron beam experiment,
   written for Perkeo III by F. Buettner and B. Maerkisch

   ************************************************************** */

#include <sstream>
//#include <cstdio>
#ifndef WIN32F
#  include "inifile.h"
#else
#  include <windows.h>
#endif

#include "Profile.h"

using namespace std;



Profile::Profile() 
  : fileName("") 
{
};


Profile::Profile(std::string iFileName) 
{
#ifdef WIN32F
	const unsigned bufsize = 4096;
	char buffer[bufsize];

	GetFullPathName(iFileName.c_str(), bufsize, buffer, NULL);
	fileName = string(buffer);
#else
	fileName = iFileName;
#endif
};


//Profile::~Profile() {};


void Profile::SetFileName(const std::string &iFileName)
{
   fileName = iFileName;
};


int Profile::GetInt(const std::string &app, const std::string &key, const int def)
{
	return GetPrivateProfileInt(app.c_str(), key.c_str(), def, fileName.c_str());
};


/*
UINT GetPrivateProfileInt(
  LPCTSTR lpAppName,  // address of section name
  LPCTSTR lpKeyName,  // address of key name
  INT nDefault,       // return value if key name is not found
  LPCTSTR lpFileName  // address of initialization filename
);
*/

std::string &Profile::GetString(const std::string &app, const std::string &key, const std::string &def)
{
	//const char defstr[] = "\0";
	const unsigned bufsize = 1024;
	char buffer[bufsize];
	DWORD ret;

	ret = GetPrivateProfileString(app.c_str(), key.c_str(), def.c_str(), buffer, bufsize, fileName.c_str());
	// TODO: throw an exception here....

	//if (strlen(buffer)==(buflen-1)) { throw};
	tempstring = string(buffer);
	return tempstring;
	//return string( buffer );
};

/*
DWORD GetPrivateProfileString(
  LPCTSTR lpAppName,        // points to section name
  LPCTSTR lpKeyName,        // points to key name
  LPCTSTR lpDefault,        // points to default string
  LPTSTR lpReturnedString,  // points to destination buffer
  DWORD nSize,              // size of destination buffer
  LPCTSTR lpFileName        // points to initialization filename
);
*/ 


double Profile::GetDouble(const std::string &app, const std::string &key, const double def)
{
    ostringstream sstr;
	double val;

    sstr << def << flush; 
	string temp(GetString( app, key, sstr.str()));
 	int ret = sscanf(temp.c_str(), "%lf", &val);
	return (ret == 1) ? val : def;
};


bool Profile::GetBool(const std::string &app, const std::string &key, const bool def)
{
	string val(GetString( app, key ));
    return (val == "" ? def : (val == "1"));
}

void Profile::SetBool(const std::string &app, const std::string &key, const bool value)
{
    SetString(app, key, value ? "1" : "0");
}


void Profile::SetInt(const std::string &app, const std::string &key, const int value)
{
    stringstream sstr;
    sstr << value << flush;
    SetString(app, key, sstr.str());
}


void Profile::SetString(const std::string &app, const std::string &key, const std::string &value)
{
	DWORD ret = WritePrivateProfileString(app.c_str(), key.c_str(), value.c_str(), fileName.c_str());
}


void Profile::SetDouble(const std::string &app, const std::string &key, const double value)
{
    stringstream sstr;
    sstr << value << flush;
    SetString(app, key, sstr.str());
}


